package com.jarvan.service.impl;

import com.jarvan.enums.ResultEnum;
import com.jarvan.exception.TrainException;
import com.jarvan.model.Station;
import com.jarvan.repository.StationRepository;
import com.jarvan.service.StationService;
import com.jarvan.utils.PinyinUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StationServiceImpl implements StationService {

    @Autowired
    StationRepository repository;
    /**
     * 新建站点
     *
     * @param station 站点对象
     * @return Station对象
     */
    @Override
    public Station save(Station station) {
        station.setStationNum(PinyinUtil.toPinyu(station.getStationName()));
        return repository.save(station);
    }

    /**
     * 通过Id查找
     *
     * @param stationId 站点id
     * @return Station对象
     */
    @Override
    public Station findById(Integer stationId) {
        Station station = repository.findById(stationId).orElse(null);
        if (station == null){
            throw new TrainException(ResultEnum.STATION_NOT_EXIST.getCode(),
                    ResultEnum.STATION_NOT_EXIST.getMessage());
        }
        return station;
    }

    /**
     * 通过站点标识查找
     *
     * @param stationNum 站点标识
     * @return Station实例对象
     */
    @Override
    public Station findByNum(String stationNum) {
        return repository.findByStationNum(stationNum);
    }

    /**
     * 站点名查找模糊查找
     *
     * @param stationName 站点名
     * @return 站点列表
     */
    @Override
    public Station findbyStationName(String stationName) {
        return repository.findByStationName(stationName);
    }

    /**
     * 分页显示所有站点
     *
     * @param pageable 分页实例
     * @return 分页对象
     */
    @Override
    public Page<Station> findAll(Pageable pageable) {
        return repository.findAll(pageable);
    }

    /**
     * 通过city查找
     *
     * @param bindCity 城市名
     * @return Station列表
     */
    @Override
    public List<Station> findByCity(String bindCity) {
        return repository.findAllByBindCity(bindCity);
    }

    /**
     * 通过省份查找
     *
     * @param bindProvince 省份名
     * @return Station列表
     */
    @Override
    public List<Station> findByProvince(String bindProvince) {
        return repository.findAllByBindProvince(bindProvince);
    }

    @Override
    public List<Station> findByStatus(Integer status) {
        return repository.findAllByStatus(status);
    }
}
